<template>
  <div class="main">
    <div class="top">
      <div class="breadcrumb">
      <p class="toptitle">首页</p>
      <i class="iconfont icon-youjiantou"></i>
      <p class="toptitle">账号管理</p>
      <i class="iconfont icon-youjiantou"></i>
      <p class="lasttitle">修改密码</p>
      </div>
<welcome></welcome>
    </div>
    <div class="table">
      <p class="list">修改密码</p>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="原密码" prop="name" class="pass">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="新密码" prop="password" class="pass">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item label="确认新密码" prop="qrpassword">
          <el-input v-model="form.qrpassword" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即修改</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import type { FormRules } from 'element-plus';
import { revise } from '../../api/account';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import Welcome from '../../components/Welcome.vue';

const route = useRouter(); // 获取路由对象
// 定义表单数据类型
interface RuleForm {
  name: string;
  password: string;
  qrpassword: string;
}

// 创建表单引用
const formRef = ref<InstanceType<typeof import('element-plus')['ElForm']>>();

// 定义表单验证规则
const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 3, max: 5, message: '原密码长度应为 3 到 5 个字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        const storedPassword = sessionStorage.getItem('password');
        if (storedPassword && value!== storedPassword) {
          callback(new Error('原密码输入错误'));
        } else {
          callback();
        }
      },
      trigger: 'blur',
    },
  ],
  password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
  ],
  qrpassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value === form.password) {
          callback();
        } else {
          callback(new Error('两次输入的密码不一致'));
        }
      },
      trigger: 'blur',
    },
  ],
});

// 定义表单数据
const form = reactive<RuleForm>({
  name: '',
  password: '',
  qrpassword: '',
});

// 修改密码事件
const onSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      console.log(form);
      const accountId = sessionStorage.getItem('accountid');
      const id = accountId? parseInt(accountId) : null;
      if (id) {
        const data = {
          id,
          newPwd: form.password,
          newPwd1: form.qrpassword,
          oldPwd: form.name,
        };
        revisePasw(data);
      }
    } else {
      console.log('error submit!');
      return false;
    }
  });
};

const revisePasw = async (data) => {
  const { code } = await revise(data);
  console.log(code);

  if (code === 0) {
    ElMessage({
      message: '修改成功',
      type: 'success',
    });
    route.push('/login');
  }
};
</script>

<style scoped>
/* 顶部面包屑 */
.toptitle {
  font-weight: bold;
}
.top {
    width: 96%;
    height: 50px;
    background-color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* 内容分布在左右两侧 */
    padding: 0 20px;
    /* 添加左右内边距 */
}

.breadcrumb {
  display: flex;
  align-items: center;
  font-size: 14px;
}
.iconfont {
  font-size: 14px;
  color: #999999;
  margin: 0 5px;
}

.lasttitle {
  color: #999999;
}
.main {
  width: 100%;
  height: 730px;
  background-color: #f0f0f0;
}
.top {
  width: 100%;
  height: 50px;
  background-color: #ffffff;
}
.table {
  margin: 20px;
  padding: 20px;
  border-radius: 5px;
  background-color: #fff;
  min-height: 600px;
}
.list {
  width: 100%;
  border-bottom: 1px solid #e1e1e1;
  font-size: 20px;
  margin-bottom: 20px;
  padding-bottom: 10px;
}
/* button */
.el-button {
  margin-left: 100px;
}
/* 将 label 右对齐 */
:deep(.el-form-item__label) {
  text-align: right;
}
.pass {
  margin-left: 30px;
}
</style>
    